﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using UIT.Markup;
using UIT.Controls.Containers;
using System.Windows.Controls;
using System.Windows;

#if !SILVERLIGHT
namespace UIT.WPF.Renderers
#else
namespace UIT.Silverlight.Renderers
#endif
{
#if !SILVERLIGHT
    class ItemsControlRenderer :
        WpfRenderer<Controls.Data.ItemsControl, ItemsControl>, IControlRenderer<Controls.Data.SelectableItemsControl>
#else
    public class ItemsControlRenderer : 
        SlRenderer<Controls.Data.ItemsControl, ItemsControl>, IControlRenderer<Controls.Data.SelectableItemsControl>
#endif
    {
        public override void Render(Controls.Data.ItemsControl control)
        {
            Controls.Containers.IControlContainer container = null;
            if (control.ItemsPanel != null && control.ItemsPanel.Template != null)
                container = (Controls.Containers.IContainer)control.ItemsPanel.Template.CreateInstance();
            if (Result == null)
                Result = new ItemsControl();
            Result.DataContext = control;
            Result.TabIndex = control.TabIndex ?? 0;
            if (container != null)
            {
                Result.ItemsPanel = new ItemsPanelTemplate();
#if !SILVERLIGHT
                Result.ItemsPanel.VisualTree = new System.Windows.FrameworkElementFactory(Manager.Render((Controls.IControl)container).GetType());
#else
            throw new NotSupportedException();
#endif
            }

            control.PropertyChanged += delegate(object sender, PropertyChangedEventArgs e)
            {
                if (e.PropertyName == "DataSource")
                {
                    ((ICollection<UIT.Controls.Data.ItemsControl.ItemWrapper>)((UIT.Controls.Data.ItemsControl)sender).Wrappers).Clear();
                    Result.Items.Clear();
                    if (((Controls.Data.IDataConsumer)sender).DataSource != null)
                        foreach (object item in ((Controls.Data.IDataConsumer)sender).DataSource.Select(new UIT.Controls.Data.SelectArgument()))
                        {
                            UIT.Controls.Template.DataTemplateInstance child = (UIT.Controls.Template.DataTemplateInstance)control.ItemTemplate.CreateInstance(item);
                            control.Wrap(child, item);
                            Result.Items.Add(Manager.Render(child));
                            //container.Children.Add(child);
                        }
                }
            };
            if (control.DataSource != null)
            {
                foreach (object item in control.DataSource.Select(new UIT.Controls.Data.SelectArgument()))
                {
                    UIT.Controls.Template.DataTemplateInstance child = (UIT.Controls.Template.DataTemplateInstance)control.ItemTemplate.CreateInstance(item);
                    control.Wrap(child, item);
                    Result.Items.Add(Manager.Render(child));
                }
            }
        }

        public override void Render(Controls.IControl control)
        {
            Controls.Data.SelectableItemsControl selectable = control as Controls.Data.SelectableItemsControl;
            if (selectable != null)
                Render(selectable);
            else
                base.Render(control);
        }

        #region IRenderer<SelectableItemsControl> Members

        public void Render(Controls.Data.SelectableItemsControl control)
        {
            Result = new ListBox();
            ((ListBox)Result).SelectedValuePath = "DataContext";
            Result.SetBinding(ListBox.SelectedValueProperty, new System.Windows.Data.Binding("SelectedView"));
            Render((Controls.Data.ItemsControl)control);
        }

        static void ItemsControlRenderer_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((UIT.Controls.Data.SelectableItemsControl)((ListBox)sender).DataContext).SelectedView = (UIT.Controls.IControl)((FrameworkElement)((ListBox)sender).SelectedItem).DataContext;
        }

        #endregion
    }
}
